package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: hea7en
 * @Date: 2024/5/21 11:17 AM
 * @Description: 购物车实现类
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 先判断购物车新添加商品是否已存在
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        List<ShoppingCart> shoppingList = shoppingCartMapper.getCartList(cart);

        // 如果已存在，则对应商品数量+1
        if (shoppingList!=null && shoppingList.size()>0) {
            ShoppingCart shoppingCart = shoppingList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            // 更新购物车
           shoppingCartMapper.updateNumberById(shoppingCart);
        }

        // 如果不存在，则添加到购物车，数量默认为1
        else{
            // 先判断是菜品还是套餐
            Long dishId = cart.getDishId();
            Long setmealId = cart.getSetmealId();
            if (dishId!=null) {
                Dish dish = dishMapper.getById(dishId);
                cart.setName(dish.getName());
                cart.setAmount(dish.getPrice());
                cart.setImage(dish.getImage());
            }
            else if(setmealId!=null){
                Setmeal Setmeal = setmealMapper.getById(setmealId);
                cart.setName(Setmeal.getName());
                cart.setAmount(Setmeal.getPrice());
                cart.setImage(Setmeal.getImage());
            }
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            // 添加到购物车
            shoppingCartMapper.add(cart);
        }
    }

    @Override
    public List<ShoppingCart> getCartList() {
       return shoppingCartMapper.getListByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void cleanCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void subOneCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        List<ShoppingCart> cartList = shoppingCartMapper.getCartList(cart);
        if (cartList!=null && cartList.size()>0) {
            ShoppingCart shoppingCart = cartList.get(0);
            Integer number = shoppingCart.getNumber();
            // 如果数量为1，则删除该商品
            if (number==1) {
                shoppingCartMapper.delete(cart);
            }
            // 如果数量大于1，则数量-1
            else if(number>1){
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
